package cn.com.blueInfo.utils.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字符串工具类
 * @ClassName: StringUtil
 * @author suxch
 * @date 2019年7月29日  上午9:23:19
 */
public class StringUtil {

    /**
     * 提取字符串中的内容到List
     * @Title: conditionToList
     * @param str
     * @param startStr
     * @param endStr
     * @return List<String>
     * @throws
     */
    public static List<String> conditionToList(String str, String startStr, String endStr) {
        return extractString(str, startStr, endStr);
    }

    /**
     * 提取字符串中的内容到String
     * @Title: conditionToString
     * @param str
     * @param startStr
     * @param endStr
     * @return String
     * @throws
     */
    public static String conditionToString(String str, String startStr, String endStr) {
        List<String> formulaList = conditionToList(str, startStr, endStr);
        String formulaStr = "";
        for (String formula : formulaList) {
            formulaStr += formula + ",";
        }
        return formulaStr.substring(0, formulaStr.length() - 1);
    }

    /**
     * 提取字符串中的内容到Array
     * @Title: conditionToArray
     * @param str
     * @param startStr
     * @param endStr
     * @return String[]
     * @throws
     */
    public static String[] conditionToArray(String str, String startStr, String endStr) {
        String formulaStr = conditionToString(str, startStr, endStr);
        String[] formulaArr = formulaStr.split(",");
        return formulaArr;
    }

    /**
     * 提取字符串中的内容到Map
     * @Title: conditionToMap
     * @param str
     * @param startStr
     * @param endStr
     * @return Map<String,String>
     * @throws
     */
    public static Map<String, String> conditionToMap(String str, String startStr, String endStr) {
        String[] formulaArr = conditionToArray(str, startStr, endStr);
        Map<String, String> formulaMap = new HashMap<String, String>();
        for (int f_i = 0, f_len = formulaArr.length; f_i < f_len; f_i++) {
            String oneStr = formulaArr[f_i];
            String[] oneStrArr = oneStr.split("=");
            if (oneStrArr.length > 1) {
                formulaMap.put(oneStrArr[0], oneStrArr[1]);
            } else {
                formulaMap.put(oneStrArr[0], oneStrArr[0]);
            }
        }
        return formulaMap;
    }

    /**
     * 提取字符串中的内容
     * @Title: extractString
     * @param str
     * @param startStr
     * @param endStr
     * @return List<String>
     * @throws
     */
    private static List<String> extractString(String str, String startStr, String endStr) {
        int index = 0, startIndex = 0, endIndex = 0;
        String replaceStartStr = "";

        List<String> formulaList = new ArrayList<String>();

        while ((index = str.indexOf(startStr, index)) != -1) {
            index += startStr.length();

            if (startIndex == 0) {
                startIndex = index;
                // 交换查找的字符串，用于处理${notes}形式的参数,这种情况用于处理开始的标志和结尾的标志不一样的参数
                if (isNotEmpty(endStr)) {
                    // 如果有第二个参数标志则替换
                    replaceStartStr = startStr;
                    startStr = endStr;
                }
            } else {
                endIndex = index;
                // 参数交换，查找以${开头的索引值
                if (isNotEmpty(endStr)) {
                    startStr = replaceStartStr;
                }
            }

            if (startIndex > 0 && endIndex > 0) {
                formulaList.add(str.substring(startIndex, endIndex - 1));
                startIndex = 0;
                endIndex = 0;
            }
        }
        return formulaList;
    }

    /**
     * 判断不为空
     * @Title: isNotEmpty
     * @param str
     * @return boolean
     * @throws
     */
    @SuppressWarnings("null")
    private static boolean isNotEmpty(String str) {
        return str != null || str.length() != 0;
    }
}

